<?php
  $db = new PDO('sqlite:supermaps.db');
  $buildings = $db->query("select id, name from Buildings")->fetchAll(PDO::FETCH_ASSOC); //Buildings(id int primary key, name varchar(32), internal_map int, x real, y real, parent_map varchar(32))
  $people = $db->query("select * from People")->fetchAll(PDO::FETCH_ASSOC); //People(id int primary key, name varchar(64), room varchar(32), building int)
  $locations = $db->query("select id, name, building from Locations")->fetchAll(PDO::FETCH_ASSOC); //Locations(id int primary key, name varchar(32), building int, category varchar(32), open time, close time)

  //Initialize the search terms
  $search_tokens = array();
  
  $token = strtok("gates", " ");
  while ($token !== false) {
    array_push($search_tokens, $token);
    $token = strtok(" ");
  }
  
  print_r($search_tokens);

  //Search the names of Buildings for a match
  foreach($buildings as $building) {
    foreach($search_tokens as $st) {
      if (strripos($building['name'], $st) !== false) {
        $result_building = $building['id']-1;
      }
    }
  }
  
  echo($result_building);
?>